package com.hthy.futures.ui.activity;

import android.os.Bundle;
import android.os.Process;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.protobuf.InvalidProtocolBufferException;
import com.hthy.futures.R;
import com.hthy.futures.api.ST_QuoteApi;
import com.hthy.futures.api.SocketQuoteManager;
import com.hthy.futures.base.BaseActivity;
import com.hthy.futures.bean.ht.BProductPriceInfo;
import com.hthy.futures.bean.ht.BRealTimeData;
import com.hthy.futures.bean.ht.BRealTimeQuotePush;
import com.hthy.futures.inter.KRequestForOneDayEvent;
import com.hthy.futures.ui.fragment.BillFragment;
import com.hthy.futures.ui.fragment.FundFragment;
import com.hthy.futures.ui.fragment.NavigationFragment;
import com.hthy.futures.ui.fragment.NewOrderFragment;
import com.hthy.futures.ui.fragment.PriceFragment;
import com.hthy.futures.ui.fragment.SeachFragment;
import com.hthy.futures.utils.DataUtils;
import com.hthy.futures.utils.DialogUtil;
import com.hthy.futures.utils.LogUtils;
import com.hthy.futures.utils.StringUtils;
import com.vilyever.socketclient.SocketClient;
import com.vilyever.socketclient.helper.SocketClientDelegate;
import com.vilyever.socketclient.helper.SocketResponsePacket;
import com.wordplat.ikvstockchart.entry.Entry;
import com.wordplat.ikvstockchart.entry.EntrySet;

import net.hthy.bean.Quote;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class MainActivity extends BaseActivity implements SocketClientDelegate {

    @Bind(R.id.iv_home)
    ImageView mIvHome;
    @Bind(R.id.iv_goods)
    ImageView mIvGoods;
    @Bind(R.id.iv_friend_circle)
    ImageView mIvFriendCircle;
    @Bind(R.id.order)
    ImageView mOrder;
    @Bind(R.id.iv_me)
    ImageView mIvMe;
    @Bind(R.id.ll_tab_goods)
    LinearLayout ll_tab_goods;
    @Bind(R.id.ll_tab_me)
    LinearLayout ll_tab_me;

    @Bind(R.id.tv_title)
    TextView mTitle;
    @Bind(R.id.iv_toolbar_left)
    ImageView mIvLeft;
    @Bind(R.id.drawer_layout)
    DrawerLayout drawer;
    @Bind(R.id.toolbar)
    Toolbar toolbar;


    PriceFragment mPriceFragment;       //
    BillFragment mBillFragment;       //
    NewOrderFragment mNewOrderFragment;       //
    FundFragment mFundFragment;       //
    SeachFragment mSeachFragment;   //
    private NavigationFragment mNavigationFragment;
    @Bind(R.id.tv_home)
    TextView mTvHome;
    @Bind(R.id.tv_bill)
    TextView mTvBill;
    @Bind(R.id.tv_order)
    TextView mTvOrder;
    @Bind(R.id.tv_fund)
    TextView mTvFund;
    @Bind(R.id.tv_search)
    TextView mTvSearch;
    private FragmentTransaction mFragementTransaction;

    long waitTime = 2000;
    long touchTime = 0;
    private static SocketClient quoteSocketClinet;
    private List<Quote.RealTimeData> mRealTimeDataList;  // 报价表集合
    private List<BRealTimeData> mMyRelTimeDatas;

    List<BRealTimeQuotePush> mBRealTimeQuotePushes = new ArrayList<>();
    private boolean isDestroy = false;  // 判断MainActivity是否被销毁，如果是的话，就不要继续
    private int mSelectIndex = -1;


    private boolean isFromDialogOneRequest = false;     // 是否来自于KDialog中一天的请求
    private boolean isFirst = true;

    public static int requestType = 0;

    public void onEventMainThread(KRequestForOneDayEvent event) {
        if (event.isFromKRequestForOneDayEvent) {
            isFromDialogOneRequest = true;
        } else {
            isFromDialogOneRequest = false;
        }
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (quoteSocketClinet == null) {
            SocketQuoteManager socketQuoteManager = new SocketQuoteManager();
            quoteSocketClinet = socketQuoteManager.getLocalSocketClient();
            socketQuoteManager.connect(quoteSocketClinet);
            quoteSocketClinet.registerSocketClientDelegate(this);
        }
    }


    public static SocketClient getSocketClient() {
        return quoteSocketClinet;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        EventBus.getDefault().register(this);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        //
        getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new NavigationFragment()).commit();

        setSelect(0);
    }

    @OnClick({R.id.ll_left, R.id.ll_tab_home, R.id.ll_tab_goods, R.id.ll_tab_add, R.id.ll_tab_circle, R.id.ll_tab_me})
    public void onclick(View view) {

        int id = view.getId();
        reSetTab();
        hideFragment();
        switch (id) {
            case R.id.ll_left:
                toggle();
                // 这个是自定义抽屉图标的小小bug,所以要设置其选中的状态
                mIvHome.setBackgroundResource(R.drawable.price_press);
                mTvHome.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case R.id.ll_tab_home:  // 价格
                setSelect(0);
                break;
            case R.id.ll_tab_goods:  // 单据
                setSelect(1);
                break;
            case R.id.ll_tab_add:   // 新订单
                setSelect(2);
                break;
            case R.id.ll_tab_circle:  // 资金
                setSelect(3);
                break;
            case R.id.ll_tab_me:   // 查询
                setSelect(4);
                break;
        }
    }


    private void setSelect(int i) {
        mFragementTransaction = getSupportFragmentManager().beginTransaction();
        reSetTab();
        hideFragment();
        if (i == 3) {
            // toolbar.setBackgroundColor(getResources().getColor(R.color.fund_head));
            toolbar.setBackground(getResources().getDrawable(R.drawable.toolbarhead));
        } else {
            toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        }
        switch (i) {
            case 0:
                if (mPriceFragment == null) {
                    mPriceFragment = new PriceFragment();
                    mFragementTransaction.add(R.id.lin_main, mPriceFragment);
                }
                mTitle.setText("价格");
                mIvLeft.setVisibility(View.VISIBLE);
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); //打开手势滑动
                mIvHome.setBackgroundResource(R.drawable.new_main_tab_price_press);
                mTvHome.setTextColor(getResources().getColor(R.color.colorPrimary));

                mFragementTransaction.show(mPriceFragment);
                break;
            case 1:
                if (mBillFragment == null) {
                    mBillFragment = new BillFragment();
                    mFragementTransaction.add(R.id.lin_main, mBillFragment);
                }
                mTitle.setText("单据");
                mIvLeft.setVisibility(View.GONE);
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);  //禁止手势滑动
                mIvGoods.setBackgroundResource(R.drawable.new_main_tab_bill_press);
                mTvBill.setTextColor(getResources().getColor(R.color.colorPrimary));
                mFragementTransaction.show(mBillFragment);
                break;
            case 2:
                //if (mNewOrderFragment == null) {
                mNewOrderFragment = new NewOrderFragment();
                mFragementTransaction.add(R.id.lin_main, mNewOrderFragment);
                //}
                mTitle.setText("新订单");
                mIvLeft.setVisibility(View.GONE);
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                mOrder.setBackgroundResource(R.drawable.new_main_tab_neworder_press);
                mTvOrder.setTextColor(getResources().getColor(R.color.colorPrimary));

                mFragementTransaction.show(mNewOrderFragment);
                if (mSelectIndex != -1) {
                    mNewOrderFragment.setSpinnerSelect(mSelectIndex);
                }
                break;
            case 3:
                if (mFundFragment == null) {
                    mFundFragment = new FundFragment();
                    mFragementTransaction.add(R.id.lin_main, mFundFragment);

                }

                mTitle.setText("");
                mIvLeft.setVisibility(View.GONE);
                //mRrHead.setVisibility(View.INVISIBLE);
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                mIvFriendCircle.setBackgroundResource(R.drawable.fund_press);
                mTvFund.setTextColor(getResources().getColor(R.color.colorPrimary));
                mFragementTransaction.show(mFundFragment);

                if (!isFirst) {
                    mFundFragment.refrsh();
                }
                isFirst = false;
                // }
                break;
            case 4:
                if (mSeachFragment == null) {
                    mSeachFragment = new SeachFragment();
                    mFragementTransaction.add(R.id.lin_main, mSeachFragment);
                }
                mTitle.setText("查询");
                mIvLeft.setVisibility(View.GONE);
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                mIvMe.setBackgroundResource(R.drawable.search_press);
                mTvSearch.setTextColor(getResources().getColor(R.color.colorPrimary));
                mFragementTransaction.show(mSeachFragment);
                break;
        }
        mFragementTransaction.commit();
    }

    /**
     * 转到新订单页面
     *
     * @param position
     */
    public void set2Fragment(int position) {
        this.mSelectIndex = position;
        setSelect(2);

    }

    /**
     * 跳到指定的页面，跟上面不一样，TODO 要一样
     *
     * @param position
     */
    public void set2Fragment2(int position) {
        setSelect(position);
    }

    private void reSetTab() {
        mIvHome.setBackgroundResource(R.drawable.new_main_tab_price);
        mIvGoods.setBackgroundResource(R.drawable.new_main_tab_bill);
        mOrder.setBackgroundResource(R.drawable.new_main_tab_neworder);
        mIvFriendCircle.setBackgroundResource(R.drawable.fund);
        mIvMe.setBackgroundResource(R.drawable.search);

        mTvHome.setTextColor(getResources().getColor(R.color.gray));
        mTvBill.setTextColor(getResources().getColor(R.color.gray));
        mTvOrder.setTextColor(getResources().getColor(R.color.gray));
        mTvFund.setTextColor(getResources().getColor(R.color.gray));
        mTvSearch.setTextColor(getResources().getColor(R.color.gray));
    }

    private void hideFragment() {
        if (mPriceFragment != null) {
            mFragementTransaction.hide(mPriceFragment);
        }
        if (mBillFragment != null) {
            mFragementTransaction.hide(mBillFragment);
        }
        if (mNewOrderFragment != null) {
            //mFragementTransaction.remove(mNewOrderFragment);
            mFragementTransaction.hide(mNewOrderFragment);
        }

        if (mFundFragment != null) {
            // mFragementTransaction.remove(mFundFragment);
            mFragementTransaction.hide(mFundFragment);
        }

        if (mSeachFragment != null) {
            mFragementTransaction.hide(mSeachFragment);
        }
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            long currentTime = System.currentTimeMillis();// 退出功能
            if ((currentTime - touchTime) >= waitTime) {
                Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
                touchTime = currentTime;
            } else {
                Process.killProcess(Process.myPid());
                finish();
            }
        }
    }

    @Override
    public void onConnected(SocketClient client) {
        LogUtils.e("hanqing connect");

        // 需要同时请求三个东西
        // 一
        byte[] baseStr2 = ST_QuoteApi.checkLogin();
        quoteSocketClinet.sendData(baseStr2);


        // 市场初始化 ---这个有什么用   ----这个放到报价表后，不然重叠起来报价表数据不显示
    }

    @Override
    public void onDisconnected(SocketClient client) {
        LogUtils.e("hanqing disconnnect");
        if (quoteSocketClinet != null) {
            quoteSocketClinet.connect();
        }
    }

    @Override
    public void onResponse(SocketClient client, @NonNull SocketResponsePacket responsePacket) {
        byte[] data = responsePacket.getData(); // 获取接收的byte数组，不为null
        String message = responsePacket.getMessage();
        if (message.indexOf("2816") != -1) {
            // 行情数据单独打印
            LogUtils.e("bingley", message);
        } else {
            LogUtils.e(message);
        }
        if (message.length() < 37) {
            return;
        }
        // String pbStr = message.substring(37);
        //byte[] bytes = pbStr.getBytes();


        // 有下标溢出的风险
        byte[] bytes = StringUtils.subBytes(data, 37, data.length - 37);

        // 如果是来自于获取一天的请求
        if (mPriceFragment.getIsFirst()) {
            isFromDialogOneRequest = false;
            getKlineDataForOneDay(responsePacket, bytes);
        } else {
            getKlineData(responsePacket, bytes);
        }

        if (getValueCheckLogin(responsePacket, bytes)) return;

        getRealTimeData(responsePacket, bytes);
        getVofferSheet(responsePacket, bytes);
        // getValueMarket(responsePacket, bytes);
    }

    /**
     * K line响应
     *
     * @param responsePacket
     * @param bytes
     */
    private void getKlineData(@NonNull SocketResponsePacket responsePacket, byte[] bytes) {
        try {
            if (responsePacket.getMessage().indexOf("1536") != -1) {
                Quote.GetHistoryKDataResponse getHistoryKDataResponse = Quote.GetHistoryKDataResponse.parseFrom(bytes);
                List<Quote.HisKDataUnit> hisKUnitList = getHistoryKDataResponse.getHisKUnitList();
                LogUtils.e("hisKUnitList" + hisKUnitList.size());

                int iPriceDecimal = getHistoryKDataResponse.getProductPriceInfo().getIPriceDecimal();

                EntrySet entrySet = new EntrySet();
                if (entrySet.getEntryList().size() > 0) {
                    entrySet.getEntryList().clear();
                }

                for (Quote.HisKDataUnit hisKDataUnit : hisKUnitList) {
                    // DataUtils.minSdf.format 这一部的操作就是为了格式化如果是 2 类型的数字，格式化成0002
                    String uiPriceTime = DataUtils.minSdf.format(hisKDataUnit.getUiPriceTime());
                    String sub_minute = uiPriceTime.substring(0, 2);
                    String sub_second = uiPriceTime.substring(2);

                    // 如果是属于天、周、月 请求
                    String uiPriceData = hisKDataUnit.getUiPriceDate() + "";
                    String data = uiPriceData.substring(4);
                    String sub_month = data.substring(0, 2);
                    String sub_day = data.substring(2);

                    double uiOpenPrice = StringUtils.moveNumberForValue(hisKDataUnit.getUiOpenPrice() + "", iPriceDecimal);
                    double uiHighPrice = StringUtils.moveNumberForValue(hisKDataUnit.getUiHighPrice() + "", iPriceDecimal);
                    double uiLowPrice = StringUtils.moveNumberForValue(hisKDataUnit.getUiLowPrice() + "", iPriceDecimal);
                    double uiClosePrice = StringUtils.moveNumberForValue(hisKDataUnit.getUiClosePrice() + "", iPriceDecimal);
                    if (requestType >= 8) {
                        if (requestType == 10) {
                            entrySet.addEntry(new Entry(Float.parseFloat(uiOpenPrice + ""), Float.parseFloat(uiHighPrice + ""), Float.parseFloat(uiLowPrice + ""),
                                    Float.parseFloat(uiClosePrice + ""), 0, sub_month));
                        } else {
                            entrySet.addEntry(new Entry(Float.parseFloat(uiOpenPrice + ""), Float.parseFloat(uiHighPrice + ""), Float.parseFloat(uiLowPrice + ""),
                                    Float.parseFloat(uiClosePrice + ""), 0, sub_month + "/" + sub_day));
                        }
                    } else {
                        if (sub_day.equals("01")&&sub_second.equals("00")) {
                            entrySet.addEntry(new Entry(Float.parseFloat(uiOpenPrice + ""), Float.parseFloat(uiHighPrice + ""), Float.parseFloat(uiLowPrice + ""),
                                    Float.parseFloat(uiClosePrice + ""), 0, sub_month + "/" + sub_day + " "+sub_minute + ":" + sub_second));
                        } else {
                            entrySet.addEntry(new Entry(Float.parseFloat(uiOpenPrice + ""), Float.parseFloat(uiHighPrice + ""), Float.parseFloat(uiLowPrice + ""),
                                    Float.parseFloat(uiClosePrice + ""), 0, sub_minute + ":" + sub_second));
                        }

                       /*entrySet.addEntry(new Entry(Float.parseFloat(hisKDataUnit.getUiOpenPrice() + ""), Float.parseFloat(hisKDataUnit.getUiHighPrice() + ""), Float.parseFloat(hisKDataUnit.getUiLowPrice() + ""),
                               Float.parseFloat(hisKDataUnit.getUiClosePrice() + ""), 0, sub_month + "/" + sub_day + " "+sub_minute + ":" + sub_second));*/
                    }

                    entrySet.computeStockIndex();
                }

                // 然后赋值给KLineDialog
                mPriceFragment.setKlineData(entrySet);

                if (mNewOrderFragment != null) {
                    mNewOrderFragment.setKlineDataForOne(entrySet);
                }
            }
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
    }


    /**
     * K line响应 (一天的请求)        搞个标记为表明是否是来自一天的请求，不然会有数据交集的现象
     *
     * @param responsePacket
     * @param bytes
     */
    private void getKlineDataForOneDay(@NonNull SocketResponsePacket responsePacket, byte[] bytes) {
        try {
            if (responsePacket.getMessage().indexOf("1536") != -1) {
                Quote.GetHistoryKDataResponse getHistoryKDataResponse = Quote.GetHistoryKDataResponse.parseFrom(bytes);
                List<Quote.HisKDataUnit> hisKUnitList = getHistoryKDataResponse.getHisKUnitList();

                int iPriceDecimal = getHistoryKDataResponse.getProductPriceInfo().getIPriceDecimal();

                EntrySet entrySet = new EntrySet();
                if (entrySet.getEntryList().size() > 0) {
                    entrySet.getEntryList().clear();
                }

                for (Quote.HisKDataUnit hisKDataUnit : hisKUnitList) {
                    //
                    String uiPriceTime = DataUtils.minSdf.format(hisKDataUnit.getUiPriceTime());
                    String sub_minute = uiPriceTime.substring(0, 2);
                    String sub_second = uiPriceTime.substring(2);

                    double uiOpenPrice = StringUtils.moveNumberForValue(hisKDataUnit.getUiOpenPrice() + "", iPriceDecimal);
                    double uiHighPrice = StringUtils.moveNumberForValue(hisKDataUnit.getUiHighPrice() + "", iPriceDecimal);
                    double uiLowPrice = StringUtils.moveNumberForValue(hisKDataUnit.getUiLowPrice() + "", iPriceDecimal);
                    double uiClosePrice = StringUtils.moveNumberForValue(hisKDataUnit.getUiClosePrice() + "", iPriceDecimal);

                    entrySet.addEntry(new Entry(Float.parseFloat(uiOpenPrice + ""), Float.parseFloat(uiHighPrice + ""), Float.parseFloat(uiLowPrice + ""),
                            Float.parseFloat(uiClosePrice + ""), 0, sub_minute + ":" + sub_second));
                }
                // 然后赋值给KLineDialog
                //EventBus.getDefault().post(new KOneDayEvent(entrySet));
                mPriceFragment.setKlineDataForOne(entrySet);

            }
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
    }


    // 报价表
    private void getVofferSheet(@NonNull SocketResponsePacket responsePacket, byte[] bytes) {
        try {
            if (responsePacket.getMessage().indexOf("512") != -1) {
                Quote.RealTimeDataMsg realTimeDataMsg = Quote.RealTimeDataMsg.parseFrom(bytes);
                mRealTimeDataList = realTimeDataMsg.getRealTimeDataList();

                // 也就是说每次在parseFrom后都赋值给另外一个新bean中
                //quoteBean.mRealTimeDataMsg


                // 自定义一个bean
                mMyRelTimeDatas = new ArrayList<>();
                for (Quote.RealTimeData realTimeData : mRealTimeDataList) {
                    int iPriceDecimal = realTimeData.getProductPriceInfo().getIPriceDecimal();

                    BRealTimeData my_B_realTimeData = new BRealTimeData();
                    BProductPriceInfo BProductPriceInfo = new BProductPriceInfo();
                    BProductPriceInfo.ss_product_code = realTimeData.getProductPriceInfo().getSsProductCode();
                    my_B_realTimeData.product_price_info = BProductPriceInfo;

                    my_B_realTimeData.ii_update_time = realTimeData.getIiUpdateTime();
                    my_B_realTimeData.ui_high_price = StringUtils.moveNumberForValue(realTimeData.getUiHighPrice()+"",iPriceDecimal);
                    my_B_realTimeData.ui_low_price = StringUtils.moveNumberForValue(realTimeData.getUiLowPrice()+"",iPriceDecimal);
                    my_B_realTimeData.ui_last_price = StringUtils.moveNumberForValue(realTimeData.getUiLastPrice()+"",iPriceDecimal);
                    my_B_realTimeData.ui_open_price = StringUtils.moveNumberForValue(realTimeData.getUiLastPrice()+"",iPriceDecimal);
                    mMyRelTimeDatas.add(my_B_realTimeData);
                }


                mPriceFragment.setData(mMyRelTimeDatas);
                LogUtils.e("bbbb","realTimeDataList" + mMyRelTimeDatas.size());

                // 市场初始化 ---这个有什么用
                //  quoteSocketClinet.sendData(ST_QuoteApi.initMarket());
                DialogUtil.dimissDialog();
            }
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();

            // 有时候会装换错误，但是再让它发送一次就正常了
            getOfferSheets(false);
            LogUtils.e("MainActivity", "InvalidProtocolBufferException");
        }
    }

    // 然后接下来得到的消息是属于行情的数据。
    private void getRealTimeData(@NonNull SocketResponsePacket responsePacket, byte[] bytes) {
        try {
            // V0.1000001650000281600000000000000000
            if (responsePacket.getMessage().indexOf("2816") != -1) {
                Quote.RealTimeQuotePushMsg realTimeQuotePushMsg = Quote.RealTimeQuotePushMsg.parseFrom(bytes);

                List<Quote.RealTimeQuotePush> rtQuoteList = realTimeQuotePushMsg.getRtQuoteList();

                // 把行情数据化为自己的东西

                // 清空容器
                if (mBRealTimeQuotePushes.size() > 0) {
                    mBRealTimeQuotePushes.clear();
                }


                for (Quote.RealTimeQuotePush realTimeQuotePush : rtQuoteList) {
                    BRealTimeQuotePush bRealTimeQuotePush = new BRealTimeQuotePush();

                    Quote.ProductPriceInfo productPriceInfo = realTimeQuotePush.getProductPriceInfo();
                    int iPriceDecimal = productPriceInfo.getIPriceDecimal();  // 这个字段决定了价格要向右移动多少位

                        /*bbb*/
                    bRealTimeQuotePush.ii_update_time = realTimeQuotePush.getIiUpdateTime();
                    bRealTimeQuotePush.ui_price = StringUtils.moveNumberForValue(realTimeQuotePush.getUiPrice() + "",iPriceDecimal);
                    BProductPriceInfo bProductPriceInfo = new BProductPriceInfo();
                    bProductPriceInfo.ss_product_code = productPriceInfo.getSsProductCode();
                    bProductPriceInfo.i_price_decimal = productPriceInfo.getIPriceDecimal();
                    bRealTimeQuotePush.product_price_info = bProductPriceInfo;

                    LogUtils.e("bingley", " IiUpdateTime " + bRealTimeQuotePush.ii_update_time + " UiPrice "
                            + bRealTimeQuotePush.ui_price + " SsProductCode " + bProductPriceInfo.ss_product_code +
                            "  IPriceDecimal " + bProductPriceInfo.i_price_decimal);
                    // 把每次的行情数据（每组几个）放在这个集合中
                    mBRealTimeQuotePushes.add(bRealTimeQuotePush);
                }

                if (mPriceFragment != null && !isDestroy) {
                    mPriceFragment.setRealTimeQuotePushes(mBRealTimeQuotePushes);
                }
                // 把 行情数据给相应的fragment
                if (mFundFragment != null && !isDestroy) {
                    mFundFragment.setRealTimeQuotePushes(mBRealTimeQuotePushes);
                }
                if (mNewOrderFragment != null && !isDestroy) {
                    mNewOrderFragment.setRealTimeQuotePushes(mBRealTimeQuotePushes);
                }

                if (mBillFragment != null && !isDestroy) {
                    mBillFragment.setRealTimeQuotePushes(mBRealTimeQuotePushes);
                }
            }

        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
    }

    private boolean getValueCheckLogin(@NonNull SocketResponsePacket responsePacket, byte[] bytes) {
        // 这个是登陆后返回的值
        try {
            // V0.1000000390003558400000000000000000
            if (responsePacket.getMessage().indexOf("35584") != -1) {
                Quote.UserLoginResponse userLoginResponse = Quote.UserLoginResponse.parseFrom(bytes);
                LogUtils.e("MainActivity", "截取后" + userLoginResponse.getSsErrorInfo() + "  " + userLoginResponse.getBLoginSuccess());

                if (userLoginResponse.getBLoginSuccess()) {
                    // 报价表 ---对应这价格内容
                    getOfferSheets(true);
                } else {
                    showToast("暂无行情数据");
                    return true;
                }

            }
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (quoteSocketClinet != null) {
            quoteSocketClinet.disconnect();
        }
        quoteSocketClinet = null;
        isDestroy = true;
    }

    /**
     * 发出报价表请求
     */
    public void getOfferSheets(boolean b) {
        if (b) {
            DialogUtil.showDialog(this);
        }
        quoteSocketClinet.sendData(ST_QuoteApi.getOfferSheet());
    }

    private void toggle() {
        int drawerLockMode = drawer.getDrawerLockMode(GravityCompat.START);
        if (drawer.isDrawerVisible(GravityCompat.START)
                && (drawerLockMode != DrawerLayout.LOCK_MODE_LOCKED_OPEN)) {
            drawer.closeDrawer(GravityCompat.START);
        } else if (drawerLockMode != DrawerLayout.LOCK_MODE_LOCKED_CLOSED) {
            drawer.openDrawer(GravityCompat.START);
        }
    }

}
